package com.sand.controller;

import com.alibaba.fastjson.JSON;
import com.sand.entity.TAuthorityEntity;
import com.sand.entity.TRoleEntity;
import com.sand.entity.TUserEntity;
import com.sand.service.IAuthService;
import com.sand.util.ModelTest;
import com.sand.util.PageModel;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping(value = "auth")
public class AuthController {
/**
 *
 *
 * @author <achao1441470436@gmail.com>
 * @since 2021/11/29/029 12:08
 */
    //authService接口
    @Resource(name = "authService")
    private IAuthService authService;

    //查询角色拥有的权限和所有权限
    @RequestMapping(value = "/queryAuthByRoleId/{id}")
    public ModelAndView queryAuthByRoleId(@PathVariable int id){
        ModelAndView mv = new ModelAndView("/layui/page/role_show2");
        List<Map<String, Object>> list = authService.queryAuthLevelAll();
        List<TAuthorityEntity> list2 = authService.queryAuthByRoleId(id);

        mv.addObject("list",list);
        mv.addObject("list2",list2);
        mv.addObject("roleId",id);
        return mv;
    }
    //分页查询权限
    @RequestMapping(value = "/queryAuthLimit")
    public void queryAuthLimit(int limit, int page, String condition, HttpServletRequest request, HttpServletResponse response)throws Exception{
        response.setCharacterEncoding("utf-8");

        PageModel<TAuthorityEntity> pm = authService.queryAuthLimit(limit, page, condition);
        ModelTest<List<TAuthorityEntity>> listData = new ModelTest<List<TAuthorityEntity>>("OK",pm.getList(),0, pm.getPagecount());
        //查询所有权限
        List<TAuthorityEntity> authList = authService.queryAuthAll();
        request.getSession().setAttribute("authList",authList);
        response.getWriter().write(JSON.toJSON(listData).toString());
    }
    //删除权限
    @RequestMapping(value = "/delAuth")
    public void delAuth(String data,HttpServletResponse response)throws Exception{
        boolean bln = authService.delAuth(data);
        if(bln){
            response.getWriter().write("OK");
        }else{
            response.getWriter().write("NO");
        }
    }
    //添加权限
    @RequestMapping(value = "/addAuth")
    public void addAuth(String data,HttpServletResponse response)throws Exception{
        boolean bln = authService.addAuth(data);
        if(bln){
            response.getWriter().write("OK");
        }else{
            response.getWriter().write("NO");
        }
    }
    //修改权限
    @RequestMapping(value = "/updateAuth",method = RequestMethod.POST)
    public void updateAuth(String data,HttpServletResponse response)throws Exception{
        boolean bln = authService.updateAuth(data);
        if(bln){
            response.getWriter().write("OK");
        }else{
            response.getWriter().write("NO");
        }
    }
    //根据id查询权限
    @RequestMapping(value = "/queryAuthById")
    public ModelAndView queryAuthById(int rid){
        TAuthorityEntity auth = authService.queryAuthById(rid);
        return new ModelAndView("/layui/page/table/edit_auth").addObject("upAuth",auth);
    }





}
